﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebMaster_WebInfo : System.Web.UI.Page
{
    private static string m_MailServer = "MailServer";
    private static string m_EmailAdmin = "EmailAdmin";

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadPage();
    }

    #region load page

    private void LoadPage()
    {
        if (!Page.IsPostBack)
        {
            bool isSuccess = true;

            GlobalClassBE.MyMessage.MgsCurrentTitle = GlobalClassBE.PageName.WebInfoTitle;
            GlobalClassBE.MyMessage.MgsCurrentMessages = "";

            ShowTextForInput();

            GssDO.DOConfiguration DO = null;

            DO = getByID(m_MailServer);
            if (DO != null && DO.IsDeleted == 0)
                txtMailServer.Text = DO.ConfigValue;
            else
                isSuccess = false;

            DO = getByID(m_EmailAdmin);
            if (DO != null && DO.IsDeleted == 0)
                txtEmailAdmin.Text = DO.ConfigValue;
            else
                isSuccess = false;

            if (!isSuccess)
                CancelPage();
        }
    }

    #endregion load page

    #region show text

    private void ShowTextForInput()
    {
        lblEmailAdmin.Text = GlobalClassBE.LabelText.EmailAdmin;
        lblMailServer.Text = GlobalClassBE.LabelText.MailServer;

        reqFormatEmailAdmin.ErrorMessage = GlobalClassBE.MyMessage.InvalidEmailAddress;
        reqField.Text = GlobalClassBE.MyMessage.MgsRequiredField;

        btnSave.Text = GlobalClassBE.ButtonText.BtnSave;
        btnCancel.Text = GlobalClassBE.ButtonText.BtnCancel;
    }

    #endregion show text

    #region ShowAll

    private GssDO.DOConfiguration getByID(string id)
    {
        GssDO.DOConfiguration DO = null;
        try
        {
            GssBL.BLConfiguration BL = new GssBL.BLConfiguration();
            DO = BL.getConfigurationByID(id);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            DO = null;
        }
        return DO;
    }
    /// <summary>
    /// show button on form (Parameter: true: show to add, false: show to edit)
    /// </summary>
    /// <param name="pFlag">type bool: true: show to add; false: show to edit</param>
    private void ShowButton(bool pFlag)
    {
        btnSave.Visible = true;
        btnCancel.Visible = true;
    }
    private void CancelPage()
    {
        Response.Redirect(@"Default.aspx", false);
    }
    private void SavePage()
    {
        lblMessage.Text = "";
        if (validateForm())
        {
            try
            {
                GssDO.DOConfiguration[] DO = new GssDO.DOConfiguration[2];
                DO[0] = fillDO(m_MailServer, txtMailServer.Text.Trim());
                DO[1] = fillDO(m_EmailAdmin, txtEmailAdmin.Text.Trim());

                GssBL.BLConfiguration BL = new GssBL.BLConfiguration();
                BL.Update(DO);
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Success;
                CancelPage();
            }
            catch (Exception exc)
            {
                string mess = exc.Message.ToString();
                lblMessage.Text = GlobalClassBE.MyMessage.MgsEdit_UnSuccess;
            }
        }
    }

    private bool validateByLabel()
    {
        bool result = true;
        try
        {
            result = true;
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }
    private bool validateForm()
    {
        bool result = true;
        try
        {
            if (!reqMailServer.IsValid)
                result = false;
            if (!reqEmailAdmin.IsValid)
                result = false;
            if (!reqFormatEmailAdmin.IsValid)
                result = false;

            if (result)
            {
                result = validateByLabel();
            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }

    private GssDO.DOConfiguration fillDO(string id, string value)
    {
        GssDO.DOConfiguration DO = getByID(id);

        if (DO != null)
            DO.ConfigValue = value;

        return DO;
    }

    #endregion ShowAll

    /***** Add Edit ******/
    protected void btnSave_Click(object sender, EventArgs e)
    {
        SavePage();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        CancelPage();
    }
}
